package cn.dakaqi.services.impl;

import cn.dakaqi.dao.FestivalDao;
import cn.dakaqi.entities.Festival;
import cn.dakaqi.services.FestivalService;
import cn.dakaqi.utils.Clock;
import cn.dakaqi.utils.DKQConstant;
import cn.dakaqi.utils.Identities;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/22.
 */
@Component(value = "festivalService")
@Transactional
public class FestivalServiceImpl implements FestivalService
{
    @Autowired
    FestivalDao festivalDao;

    @Override
    public void create(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);

        String code = "ADS" + Identities.randomBase62(7);
        String img = jsonObject.getString("img");
        String link = jsonObject.getString("link");
        String note = jsonObject.getString("note");
        int status = DKQConstant.DISPLAY_NO;
        Date createTime = Clock.DEFAULT.getCurrentDate();
        Festival festival = new Festival();
        festival.setCode(code);
        festival.setImg(img);
        festival.setLink(link);
        festival.setStatus(status);
        festival.setCreateTime(createTime);
        festival.setNote(note);
        this.save(festival);
    }

    @Override
    public void save(Festival festival) throws ServiceRuntimeException
    {
        festivalDao.save(festival);
    }

    @Override
    public void updateStatus(String code, int status) throws ServiceRuntimeException
    {
        Festival festival = findByCode(code);
        if(null != festival)
        {
            festival.setStatus(status);
            this.update(festival);
        }

    }

    @Override
    public Festival findOne(Long id) throws ServiceRuntimeException
    {
        return this.findOne(id);
    }

    @Override
    public Festival findOne() throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "status");
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.DISPLAY_YES));
        Specification<Festival> spec = DynamicSpecifications.bySearchFilter(filters.values(), Festival.class);
        List<Festival> list = festivalDao.findAll(spec, sort);
        if(null == list || list.size() == 0)
            return null;
        return list.get(0);
    }

    @Override
    public Festival findByCode(String code) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("code", new SearchFilter("code", SearchFilter.Operator.EQ, code));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, DKQConstant.DISPLAY_YES));
        Specification<Festival> spec = DynamicSpecifications.bySearchFilter(filters.values(), Festival.class);
        List<Festival> list = festivalDao.findAll(spec);
        if(null == list || list.size() == 0)
            return null;
        return list.get(0);
    }

    @Override
    public Page<Festival> findAll(int pageNumber) throws ServiceRuntimeException
    {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageRequest pageRequest = new PageRequest(pageNumber - 1, 20, sort);
        Specification<Festival> spec = DynamicSpecifications.bySearchFilter(null, Festival.class);
        return this.festivalDao.findAll(spec, pageRequest);
    }

    @Override
    public void updateStatus(String param) throws ServiceRuntimeException
    {
        JSONObject jsonObject = JSON.parseObject(param);

        String code = jsonObject.getString("code");
        int status = jsonObject.getIntValue("status");
        this.updateStatus(code,status);
    }

    @Override
    public void del(String code) throws ServiceRuntimeException
    {
        Festival festival = this.findByCode(code);
        if(null != festival)
        {
            festival.setDelStatus(DKQConstant.DEL_YES);
            this.update(festival);
        }
    }

    @Override
    public void del(Long id) throws ServiceRuntimeException
    {
        Festival festival = this.findOne(id);
        if(null != festival)
        {
            festival.setDelStatus(DKQConstant.DEL_YES);
            this.update(festival);
        }
    }

    @Override
    public void update(Festival festival) throws ServiceRuntimeException
    {
        if(null != festival)
            this.festivalDao.save(festival);
    }
}
